﻿using System;
using System.Collections.Generic;
using System.Text;

namespace X.Pager
{
    public class MyPagerBase
    {
        protected string _defaultCssName = "Pager";
        protected string[] PAGE_SIZE = new string[] { "10", "20", "50", "100" };
        public bool PageSizeEnable = true;
        protected string[] parm;
        protected string[] parmvalue;
        protected StringBuilder parmlink = new StringBuilder();

        public virtual string DrawPager()
        {
            return DrawPager("");
        }

        public virtual string DrawPager(string customerCssName)
        {
            return DrawPager(_defaultCssName, customerCssName);
        }

        public virtual string DrawPager(string defaultCssName, string customerCssName)
        {
            return DrawPager(_defaultCssName, customerCssName);
        }

        public virtual string DrawPager(string customerCssName, Dictionary<string, string> requestParms)
        {
            foreach (KeyValuePair<string, string> kvp in requestParms)
            {
                parmlink.Append("&" + kvp.Key + "=" + kvp.Value);
            }
            return DrawPager(customerCssName);
        }

        protected string DrawPagerJs(int pageCount, int perPageInGroup, int customerPageSize, int count)
        {
            StringBuilder js = new StringBuilder("<script type=\"text/javascript\">\r\n");
            js.Append("  var pageCount = " + pageCount + ";\r\n");
            js.Append("  var perPageInGroup = " + perPageInGroup + ";\r\n");
            js.Append("  var groupCount = Math.floor(pageCount / perPageInGroup) + 1;\r\n");
            js.Append("  var pagesize = " + customerPageSize + ";\r\n");
            js.Append("  var extendLink = \"" + parmlink.ToString() + "\";\r\n");
            js.Append("  var count = \"" + count + "\";\r\n");

            js.Append("  function Draw(groupIndex,pageIndex,sid,count) {\r\n");
            js.Append("    var html = \"\";\r\n");
            js.Append("    var prefix = \"\";\r\n");

            js.Append("    if (groupIndex > 1) {\r\n");
            js.Append("      prefix = \"<a href=\\\"?s=1&g=1\"+ extendLink + \"&z=" + customerPageSize + "\\\">&lt;&lt;</a>\";\r\n");
            js.Append("      var previous = groupIndex - 1;\r\n");
            js.Append("      prefix += \"<a href=\\\"?s=\" + (previous * perPageInGroup) + \"&g=\" + previous + extendLink + \"&z=" + customerPageSize + "\\\">...</a>\";\r\n");
            js.Append("    }\r\n");

            js.Append("    for (var i = 0; i < perPageInGroup; i++) {\r\n");
            js.Append("      var index = parseInt((groupIndex - 1) * perPageInGroup) + parseInt(i + 1);\r\n");
            //js.Append("      alert(groupIndex);\r\n");
            //js.Append("      alert(parseInt((groupIndex - 1) * perPageInGroup));\r\n");
            //js.Append("      alert(parseInt(i + 1));\r\n");
            js.Append("      if (pageIndex == index) {\r\n");
            js.Append("        html += \"<span>\" + index + \"</span>\";\r\n");
            js.Append("      }\r\n");
            js.Append("      else {\r\n");
            js.Append("        html += \"<a href=\\\"?s=\" + index + \"&g=\" + groupIndex + extendLink + \"&z=" + customerPageSize + "\\\">\" + index + \"</a>\";\r\n");
            js.Append("      }\r\n");
            js.Append("      if (index == pageCount) {\r\n");
            js.Append("        break;\r\n");
            js.Append("      }\r\n");
            js.Append("    }\r\n");

            js.Append("    var suffix = \"\";\r\n");
            js.Append("    if (groupIndex < groupCount) {\r\n");
            js.Append("      var next = groupIndex + 1;\r\n");
            js.Append("      suffix += \"<a href=\\\"?s=\" + groupIndex * perPageInGroup + \"&g=\" + next + extendLink + \"&z=" + customerPageSize + "\\\">...</a>\";\r\n");
            js.Append("      suffix += \"<a href=\\\"?s=\" + pageCount + \"&g=\" + groupCount + extendLink + \"&z=" + customerPageSize + "\\\">&gt;&gt;</a>\";\r\n");
            js.Append("    }\r\n");

            js.Append("    var redirect = \"\";\r\n");
            js.Append("    redirect += \"<input type=\\\"text\\\" id=\\\"PageNumber\\\" name=\\\"PageNumber\\\" style=\\\"font:12px '宋体'; width:24px; height:14px;\\\" />&nbsp;<input type=\\\"button\\\" value=\\\"GO\\\" style=\\\"width:24px; height:20px;\\\" onclick=\\\"GoPage();\\\" />\";\r\n");
            js.Append("\r\n");

            js.Append("    var selection = \"\";\r\n");
            js.Append("    selection += \"<select id=\\\"z\\\" name=\\\"z\\\" \";\r\n");
            js.Append("    selection += sid == 1 ? \" onchange=\\\"chgpagesize(this.value);\\\" \" : \" disabled=\\\"disabled\\\" \";\r\n");
            js.Append("    selection += \" >\";\r\n");

            foreach (string size in PAGE_SIZE)
            {
                js.Append("    selection += \"<option value=\\\"" + size + "\\\" \";\r\n");
                if (size == customerPageSize.ToString())
                    js.Append("    selection += \"selected=\\\"selected\\\" \";\r\n");
                js.Append("    selection += \">" + size + "</option>\";\r\n");
            }

            //js.Append("    for (var i = 0; i < 10; i++) {
            //js.Append("        selection += "  <option value=\"" + i + "\">" + i + "</option>\r\n";
            //js.Append("    }

            js.Append("    selection += \"</select>\";\r\n");

            js.Append("    var pager = prefix + html + suffix + redirect + selection + \"&nbsp;总数：\" + count + \"\";\r\n");
            js.Append("\r\n");
            js.Append("    document.getElementById(\"pager\").innerHTML = pager;\r\n");
            js.Append("  }\r\n");

            js.Append("\r\n");

            js.Append("  function GoPage() {\r\n");
            js.Append("    var pageNumber = document.getElementById(\"PageNumber\").value;\r\n");
            js.Append("    if (isNaN(pageNumber)) {\r\n");
            js.Append("      alert(\"请输入数字\");\r\n");
            js.Append("    } else {\r\n");
            js.Append("      pageNumber = parseInt(pageNumber);\r\n");
            js.Append("      if (pageNumber <= 0 || pageNumber > pageCount) {\r\n");
            js.Append("        alert(\"页面输入错误\");\r\n");
            js.Append("      } else {\r\n");
            js.Append("        //        alert(Math.floor(pageNumber/perPageInGroup));\r\n");
            js.Append("        var groupIndex = Math.floor(pageNumber / perPageInGroup) + 1;\r\n");
            js.Append("        window.location.replace(\"?s=\" + pageNumber + \"&g=\" + groupIndex + \"&z=" + customerPageSize + parmlink.ToString() + "\");\r\n");
            js.Append("      }\r\n");
            js.Append("    }\r\n");
            js.Append("  }\r\n");

            js.Append("\r\n");

            js.Append("function chgpagesize(value){\r\n");
            js.Append("  window.location.replace('?z='+value+'" + parmlink.ToString() + "');\r\n");
            js.Append("}\r\n");

            js.Append("</script>\r\n");

            return js.ToString();
        }

        protected string DrawPagerCss()
        {
            StringBuilder css = new StringBuilder("<style type=\"text/css\">\r\n");
            css.Append("  #pager a{ margin-right:12px; }\r\n");
            css.Append("  #pager a:link{ color:#5e5e5e; }\r\n");
            css.Append("  #pager a:visited{ color:#5e5e5e; }\r\n");
            css.Append("  #pager a:hover{ color:#8c0000; text-decoration:underline; }\r\n");
            css.Append("  #pager span{ margin-right:12px; color:Red; }\r\n");
            css.Append("  #pager select{ width:45px; font:12px '宋体'; }\r\n");
            css.Append("</style>\r\n");
            return css.ToString();
        }
    }
}
